package com.ssyt.ec.sys.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.ec.common.AppServlet;
import com.ssyt.ec.common.util.DBUtil;
import com.ssyt.ec.common.util.FastjsonUtil;
import com.ssyt.ec.sys.entity.SysRoleEntity;
import com.ssyt.ec.sys.service.SysRoleService;

@WebServlet("/SysRoleServlet")
public class SysRoleServlet extends AppServlet {
	private static final long serialVersionUID = 1L;

	public SysRoleServlet() {
		super();
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("listRole".equals(command)) {
			listRole(request, response);
		} else if ("addRole".equals(command)) {
			addRole(request, response);
		} else if ("getRole".equals(command)) {
			getRole(request, response);
		} else if ("updaterole".equals(command)) {
			updateRole(request, response);
		} else if ("updateOpRole".equals(command)) {
			updateOpRole(request, response);
		} else if ("listRoleclo".equals(command)) {
			listRoleclo(request, response);
		}
	}

	/**
	 * 显示禁用角色
	 * 
	 * @param request
	 * @param response
	 */
	private void listRoleclo(HttpServletRequest request,
			HttpServletResponse response) {
		SysRoleService roleService = new SysRoleService();
		List<SysRoleEntity> listrole = roleService.listRoleclo();
		print(response,
				FastjsonUtil.toJsonWithDateFormat(listrole, "yyyy-MM-dd"));
	}

	/**
	 * 修改状态
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 */
	private void updateOpRole(HttpServletRequest request,
			HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		String value = request.getParameter("value");
		int value1 = 0;
		if ("禁用".equals(value)) {
			value1 = 2;
		} else if ("启用".equals(value)) {
			value1 = 1;
		}
		SysRoleService roleService = new SysRoleService();
		int data = roleService.updateOpRole(id, value1);
		print(response, FastjsonUtil.toJson(data));
	}

	/**
	 * 角色信息修改
	 * 
	 * @param request
	 * @param response
	 */
	private void updateRole(HttpServletRequest request,
			HttpServletResponse response) {
		SysRoleService roleService = new SysRoleService();
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("roleName");
		String[] menus = request.getParameterValues("menu");
		if (name != null && menus != null) {
			StringBuffer memuIds = new StringBuffer();
			for (int i = 0; i < menus.length; i++) {
				if (i == menus.length - 1) {
					memuIds.append(menus[i]);
				} else {
					memuIds.append(menus[i] + ",");
				}
			}
			String memu = memuIds.toString();
			int data = roleService.updateRole(id, name, memu);
			print(response, FastjsonUtil.toJson(data));
		}
	}

	/**
	 * 角色id查询
	 * 
	 * @param request
	 * @param response
	 */
	private void getRole(HttpServletRequest request,
			HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		SysRoleEntity role = DBUtil.getUniqueResult(SysRoleEntity.class,
				"SELECT * FROM sys_role WHERE id=?", id);
		System.out.println(role.getMenu_ids());
		if (role != null) {
			print(response, FastjsonUtil.toJson(role));

		}

	}

	/**
	 * 角色新增
	 * 
	 * @param request
	 * @param response
	 */
	private void addRole(HttpServletRequest request,
			HttpServletResponse response) {
		SysRoleService roleService = new SysRoleService();
		String roleName = request.getParameter("roleName");
		// String operate=request.getParameter("operate");
		// 操作者id暂时默认为1
		int operate = 1;
		String[] menus = request.getParameterValues("menuAdd");
		if (roleName != null && menus != null) {
			StringBuffer memuIds = new StringBuffer();
			for (int i = 0; i < menus.length; i++) {
				if (i == menus.length - 1) {
					memuIds.append(menus[i]);
				} else {
					memuIds.append(menus[i] + ",");
				}
			}
			String memu = memuIds.toString();
			int data = roleService.addRole(roleName, operate, memu);
			print(response, FastjsonUtil.toJson(data));
		}
	}

	/**
	 * 显示启用角色
	 * 
	 * @param request
	 * @param response
	 */
	private void listRole(HttpServletRequest request,
			HttpServletResponse response) {
		SysRoleService roleService = new SysRoleService();

		String name = request.getParameter("SearchName");
		// System.out.println(name);
		if (name != null) {
			List<SysRoleEntity> listrole = roleService.selectRole(name);
			print(response,
					FastjsonUtil.toJsonWithDateFormat(listrole, "yyyy-MM-dd"));
		} else {
			List<SysRoleEntity> listrole = roleService.listRoleop();
			print(response,
					FastjsonUtil.toJsonWithDateFormat(listrole, "yyyy-MM-dd"));
		}
	}

}
